<?php
namespace App\Services\Admin;

use Illuminate\Support\Facades\DB;

class TmpService extends BaseService
{
    /**
     * 场景
     */

    /**
     * 获取列表
     * @param array $pages【start起始页面，limit每页记录数】
     * @return array
     */
    public function getList($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbTmp);
        if (isset($pas['uid']) && $pas['uid']) {
            $db = $db->where('uid', $pas['uid']);
        }
        $total = $db->count();
        $tmps = $db->skip($start)->take($pages['limit'])
                        ->orderBy('id', 'desc')
                        ->get();
        if (!count($tmps)) {
            return array('code' => -2, 'msg' => '场景不存在');
        }
        $arys = [];
        foreach ($tmps as $k => $v) {
            $v = (array) $v;
            $thumb = file_exists($v['thumb']) ? $v['thumb'] : '';
            $sceneOne = (array) $this->dbConn($this->dbScene)
                ->where('id', $v['scene_id'])
                ->first();
            $attrs = $this->dbConn($this->dbTmpAttr)
                ->select('id', 'tmp_id', 'attr_name', 'val')
                ->where('del', 1)
                ->where('tmp_id', $v['id'])
                ->get();
            $scene_name = empty($sceneOne) ? '-' : $sceneOne['title'];
            $arys[] = [
                'id' => $v['id'],
                'uid' => $v['uid'],
                'code' => $v['code'],
                'title' => $v['title'],
                'scene_name' => $scene_name,
                'uptime' => $v['uptime'],
                'attrs' => $attrs,
            ];
        }
        return array('code' => 0, 'msg' => 'success', 'data'=>$arys, 'total' => $total);
    }

    /**
     * 获取列表
     * @param int $id
     */
    public function getOne($id)
    {
        $res = (array) $this->dbConn($this->dbTmp)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return array('code' => -2, 'msg' => '没有记录');
        }
        $uOne = (array) $this->dbConn($this->dbUser)->where('id', $res['uid'])->first();
        $ucode = empty($uOne) ? '-' : $uOne['code'];
        $imgs = $this->dbConn($this->dbTmpimg)
            ->select('id', 'link')
            ->where('del', 1)
            ->where('tmp_id', $id)
            ->get();
        $attrs = $this->dbConn($this->dbTmpAttr)
            ->select('id', 'tmp_id', 'attr_name', 'val')
            ->where('del', 1)
            ->where('tmp_id', $id)
            ->get();
        $arys = [
            'id' => $res['id'],
            'uid' => $res['uid'],
            'ucode' => $ucode,
            'code' => $res['code'],
            'title' => $res['title'],
            'tmp_code' => $res['tmp_code'],
            'uptime' => $res['uptime'],
            'imgs' => $imgs,
            'attrs' => $attrs,
        ];
        return array('code' => 0, 'msg' => 'success', 'data'=>$arys);
    }

    //添加
    public function add($title)
    {
        $res = (array) $this->dbConn($this->dbTmp)
            ->where('del', 1)
            ->where('title', $title)
            ->first();
        if (!empty($res)) {
            return $this->toback(-2, '');
        }
        $lastid = $this->dbConn($this->dbTmp)->insertGetId([
            'title' => $title, 'tmp_code' => toCode(),
            'addtime' => toDate(), 'uptime' => toDate(),
        ]);
        if (!$lastid) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 修改
     * @param int $id
     * @param string $title 标签名称
     * @return array
     */
    public function modify($id, $title)
    {
        //有没有tmp
        $tmpOne = (array) $this->dbConn($this->dbTmp)
            ->where('del', 1)
            ->where('id', $id)
            ->first();
        if (empty($tmpOne)) {
            return $this->toback(-2, '没有记录');
        }
        $tmpData = [
            'title' => $title,
            'uptime' => toDate(),
        ];
        $resTmp = $this->dbConn($this->dbTmp)->where('id', $id)->update($tmpData);
        if (!$resTmp) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
//        DB::beginTransaction();
//        try {
//            //tmp
//            DB::commit();
//            return array('code' => 0, 'msg' => 'success');
//        } catch (\Exception $e) {
//            DB::rollBack();
//            echo $e->getMessage();
//            return array('code' => -1, 'msg' => 'fail');
//        }
    }

    /**
     * 图片上传
     * @param int $id
     * @param string $img
     */
    public function uploadImg($id, $img)
    {
        return $this->dbConn($this->dbTmpimg)->insertGetId([
            'tmp_id' => $id, 'link' => $img, 'addtime' => toDate(),
        ]);
    }

    /**
     * 属性修改
     * @param int $id 属性ID
     * @param array $pas
     */
    public function attrAdd($pas)
    {
        $res = (array) $this->dbConn($this->dbTmpAttr)
            ->where('del', 0)
            ->where('tmp_id', $pas['tmp_id'])
            ->where('attr_name', $pas['attr_name'])
            ->first();
        if (!empty($res)) {
            return $this->toback(-2, '存在同名记录');
        }
        $pas['uptime'] = $pas['addtime'] = toDate();
        $lastid = $this->dbConn($this->dbTmpAttr)->insertGetId($pas);
        if (!$lastid) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 属性修改
     * @param int $id 属性ID
     * @param array $pas
     */
    public function attrModify($id, $pas)
    {
        $res = (array) $this->dbConn($this->dbTmpAttr)
            ->where('del', 1)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $pas['uptime'] = toDate();
        $up = $this->dbConn($this->dbTmpAttr)->where('id', $id)->update($pas);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 关注列表
     * @param int $tmpId 模板ID
     * @param array $pages 分页参数：start起始页面，limit每页记录数
     * @return array
     */
    public function getFollowList($tmpId, $pages)
    {
        $db = $this->dbConn($this->dbTmpFollow);
        if ($tmpId) {
            $db = $db->where('tmp_id', $tmpId);
        }
        $total = $db->count('id');
        $start = ($pages['start'] - 1) * $pages['limit'];
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get()->toArray();
        if (!count($res)) {
            return array('code' => -2, 'msg' => '没有关注');
        }
        $rst = [
            'data' => $res,
            'total' => $total,
        ];
        return array('code' => 0, 'msg' => 'success', 'data' => $rst);
    }

    /**
     * 获取评论列表
     * @param int $tmpId 模板ID
     * @param array $pages 分页参数：start起始页面，limit每页记录数
     * @return array
     */
    public function getCommentList($tmpId, $pages)
    {
        $total = $this->dbConn($this->dbTmpComm)
            ->where('del', 1)
            ->where('tmp_id', $tmpId)
            ->count('id');
        $start = ($pages['start'] - 1) * $pages['limit'];
        $comOnes = $this->dbConn($this->dbTmpComm . ' as t1')
            ->select('t1.*', 't2.title')
            ->leftJoin($this->dbTmp . ' as t2', 't2.id', '=', 't1.tmp_id')
            ->where('t1.del', 1)
            ->where('t1.tmp_id', $tmpId)
            ->skip($start)->take($pages['limit'])
            ->orderBy('t1.id', 'desc')
            ->get();
        if (!count($comOnes)) {
            return array('code' => -2, 'msg' => '没有评论');
        }
        $rst = [
            'data' => $comOnes,
            'total' => $total,
        ];
        return array('code' => 0, 'msg' => 'success', 'data' => $rst);
    }
}